home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / datelib.exe / TODAY.C < prev    next >
C/C++ Source or Header  |  1992-03-27  |  4KB  |  137 lines

  1. static char today_prog[] = "@(#)Aktuelles Datum ermitteln";
  2. static char today_ver[]  = "@(#)v1.10/kr ; 10.06.91";
  3. /* today        Ermitteln des aktuellen Datums aus der Systemzeit.
  4. **
  5. ** Autor        Klaus Rath
  6. **
  7. ** Deklaration  char *today(int format);
  8. **
  9. ** Beschreibung Die Funktion liest die Systemzeit aus und gibt einen String
  10. **              im gewünschten Format zurück. Die aufrufende Funktion muß
  11. **              deshalb sicherstellen, daß ein char-Array ausreichender
  12. **              Größe definiert ist, oder sich zum Aufrufzeitpunkt genügend
  13. **              Speicherplatz per malloc besorgen.
  14. **              Erlaubte Formate, ihre Ausprägung und ihre Arraylänge:
  15. **              1                 tt.mm.jj            9
  16. **              2                 mm/tt/jj            9
  17. **              3                 jjmmtt              7
  18. **              4                 tt.mm.jjjj          11
  19. **              5                 mm/tt/jjjj          11
  20. **              6                 jjjjmmtt            9
  21. **              7                 tt.mm.              7
  22. **              8                 mm/tt               6
  23. **              Im Fehlerfall gibt today einen (char *)NULL zurück!
  24. **
  25. ** Änderungen   1.00 ; 12.04.91
  26. **              - Erste Version
  27. **              1.10 ; 10.06.91
  28. **              - Kurzformate 7 und 8 eingebaut, Umkopieren durch sprintf()
  29. **                ersetzt.
  30. */
  31.  
  32. #include <stdio.h>
  33. #include <time.h>
  34. #include <string.h>
  35. #include <sys\types.h>
  36. #ifdef __TURBOC__
  37. #define ANSI
  38. #define MSDOS
  39. #include <stdlib.h>
  40. #endif
  41. #include "datum.h"
  42.  
  43. #ifdef ANSI
  44. char *today(int format)
  45. #else
  46. char *today(format)
  47. int format;
  48. #endif
  49. {
  50.     char      tag[3];
  51.     char      monat[3];
  52.     char      jahr[3];
  53.     char      jh[3];
  54.     char      jahreszahl[5];
  55.     int       tag_z;
  56.     int       monat_z;
  57.     int       jahr_z;
  58.     char      systemzeit_s[26];
  59.     time_t    systemzeit;
  60.     struct tm *heute;
  61.     char      rueckgabe[11];
  62.  
  63.     /* Jahrhundert aus der Systemzeit ermitteln :
  64.     */
  65.     time(&systemzeit);
  66.     strcpy(systemzeit_s,ctime(&systemzeit));
  67.     jh[0] = systemzeit_s[20];
  68.     jh[1] = systemzeit_s[21];
  69.     jh[2] = '\0';
  70.  
  71.     /* Restliche Elemente aus der localtime :
  72.     */
  73.     heute   = localtime(&systemzeit);
  74.     tag_z   = heute->tm_mday;
  75.     monat_z = heute->tm_mon + 1;
  76.     jahr_z  = heute->tm_year;
  77.  
  78.     /* Formatiert umkopieren (2stellig!) :
  79.     */
  80.     sprintf(tag,  "%02d",tag_z);
  81.     sprintf(monat,"%02d",monat_z);
  82.     sprintf(jahr, "%02d",jahr_z);
  83.  
  84.     /* Je nach Format Jahrhundert ergänzen :
  85.     */
  86.     if ( format >= 1 && format <= 3 ) {
  87.         strcpy(jahreszahl,jahr);
  88.     }
  89.     else if ( format >= 4 && format <= 6 ) {
  90.         strcpy(jahreszahl,jh);
  91.         strcat(jahreszahl,jahr);
  92.     }
  93.     else if ( format == 7 || format == 8 ) {
  94.         ;/* Keine Ergänzung! */
  95.     }
  96.     else
  97.         return(NULL);
  98.  
  99.     /* Rückgabe nach Vorgabe zusammensetzen :
  100.     */
  101.     if ( format == DE_KURZ || format == DE_LANG ) {
  102.         strcpy(rueckgabe,tag);
  103.         strcat(rueckgabe,".");
  104.         strcat(rueckgabe,monat);
  105.         strcat(rueckgabe,".");
  106.         strcat(rueckgabe,jahreszahl);
  107.     }
  108.     else if ( format == AM_KURZ || format == AM_LANG ) {
  109.         strcpy(rueckgabe,monat);
  110.         strcat(rueckgabe,"/");
  111.         strcat(rueckgabe,tag);
  112.         strcat(rueckgabe,"/");
  113.         strcat(rueckgabe,jahreszahl);
  114.     }
  115.     else if ( format == DB_KURZ || format == DB_LANG ) {
  116.         strcpy(rueckgabe,jahreszahl);
  117.         strcat(rueckgabe,monat);
  118.         strcat(rueckgabe,tag);
  119.     }
  120.     else if ( format == DE_AKTJAHR ) {
  121.         strcpy(rueckgabe,tag);
  122.         strcat(rueckgabe,".");
  123.         strcat(rueckgabe,monat);
  124.         strcat(rueckgabe,".");
  125.     }
  126.     else if ( format == DE_AKTJAHR ) {
  127.         strcpy(rueckgabe,monat);
  128.         strcat(rueckgabe,"/");
  129.         strcat(rueckgabe,tag);
  130.     }
  131.     else
  132.         return(NULL);
  133.  
  134.     return(rueckgabe);
  135.  
  136. } /* ENDE: today() */
  137.